package com.cet.algorithm.test.二叉树.二叉树通用迭代遍历;

import com.cet.algorithm.test.二叉树.TreeNode;
import com.cet.algorithm.test.二叉树.TreeOperation;

import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

/**
 * @program: cet-practice
 * @description: 通用迭代遍历
 * @author: 陈恩涛
 * @create: 2023-05-24 12:02
 **/
public class IterateTra {

    public List<Integer> preorderTraversal(TreeNode root) {

        if (root == null) {
            return new LinkedList<>();
        }

        // 频繁插入操作，使用LinkedList
        LinkedList<Integer> result = new LinkedList<>();

        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);

        while (!stack.isEmpty()) {
            TreeNode node = stack.peek();

            if (node != null) {
                // 当前节点后续需要再次入栈
                TreeNode treeNode = stack.pop();

                if (treeNode.right != null) {
                    stack.push(treeNode.right);
                }

                if (treeNode.left != null) {
                    stack.push(treeNode.left);
                }

                stack.push(treeNode);
                stack.push(null);

            } else {
                // null出栈
                stack.pop();
                // 真正包含值的节点出栈
                TreeNode treeNode = stack.pop();
                result.add(treeNode.val);
            }
        }

        return result;
    }

    public List<Integer> inorderTraversal(TreeNode root) {
        if (root == null) {
            return new LinkedList<>();
        }

        // 频繁插入操作，使用LinkedList
        LinkedList<Integer> result = new LinkedList<>();

        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);

        while (!stack.isEmpty()) {
            TreeNode node = stack.peek();

            if (node != null) {
                // 当前节点后续需要再次入栈
                TreeNode treeNode = stack.pop();

                if (treeNode.right != null) {
                    stack.push(treeNode.right);
                }

                stack.push(treeNode);
                stack.push(null);

                if (treeNode.left != null) {
                    stack.push(treeNode.left);
                }
            } else {
                // null出栈
                stack.pop();
                // 真正包含值的节点出栈
                TreeNode treeNode = stack.pop();
                result.add(treeNode.val);
            }
        }

        return result;
    }

    public List<Integer> postorderTraversal(TreeNode root) {
        if (root == null) {
            return new LinkedList<>();
        }

        // 频繁插入操作，使用LinkedList
        LinkedList<Integer> result = new LinkedList<>();

        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);

        while (!stack.isEmpty()) {
            TreeNode node = stack.peek();

            if (node != null) {
                // 当前节点后续需要再次入栈
                TreeNode treeNode = stack.pop();

                stack.push(treeNode);
                stack.push(null);

                if (treeNode.right != null) {
                    stack.push(treeNode.right);
                }

                if (treeNode.left != null) {
                    stack.push(treeNode.left);
                }
            } else {
                // null出栈
                stack.pop();
                // 真正包含值的节点出栈
                TreeNode treeNode = stack.pop();
                result.add(treeNode.val);
            }
        }

        return result;
    }

    public static void main(String[] args) {
        IterateTra iterateTra = new IterateTra();
        TreeNode root = new TreeNode(1, new TreeNode(2, new TreeNode(4), new TreeNode(5)), new TreeNode(3, new TreeNode(6), new TreeNode(7)));
        TreeOperation.show(root);
        System.out.println("先序遍历：" + iterateTra.preorderTraversal(root));
        System.out.println("中序遍历：" + iterateTra.inorderTraversal(root));
        System.out.println("后序遍历：" + iterateTra.postorderTraversal(root));
    }
}
